/*
 * @Author: your name
 * @Date: 2021-12-18 22:57:40
 * @LastEditTime: 2021-12-22 15:27:28
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \am\am-server\app\service\student.js
 */
const { Service } = require('egg')
const BussinessError = require('../utils/BussinessError')

class StudentService extends Service {

  async saveOrUpdate (row) {
    const { mysql } = this.app
    try {
      if (row.id) {
        // 更新 {id:1,name:"terry",no:2021110022}  update school_student set name='terry',no=2021 where id = 1
        await mysql.update('school_student', row)
      } else {
        await mysql.insert('school_student', row)
      }
    } catch (error) {
      console.log(error);
      throw new BussinessError('操作失败')
    }
  }
  
   // 查询所有学生
   async findstudentAll () {
    const { mysql } = this.app
    const students = await mysql.select('school_student')
    return students
  }


  async pageQuery ({ page, pageSize }) {
    const { mysql } = this.app;
    
    //1. 查询分页数据
    let sql_select = `select * from school_student limit ${(page - 1) * pageSize},${pageSize}`
    const students = await mysql.query(sql_select)

    //2. 统计数量
    const sql = `select count(*) as total from school_student`
    // 获取分页查询的数据条数
    const [{ total }] = await mysql.query(sql)
    // 返回
    return {
      page,
      pageSize,
      total,
      list: students
    }
  }
  
// 根据no删除学生
async deletestudentByno (no) {
  const { mysql } = this.app
  try {
    await mysql.delete('school_student', { no })
  } catch (error) {
    throw new BussinessError('删除失败')
  }
}

  //根据no查询学生详细信息
  async findByno (no) {
    const { mysql } = this.app
    const nostudent = await mysql.select('school_student', {
      where: { no }
    })
    return {
      nostudent
    }
  }

}
module.exports = StudentService;